// pages/service/bankcard-binding/index.js
var app = getApp(),
  api = require("../../../api.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    bankArr: [
     
    ],
    bank: { bankname:'请选择开户行'},
    bankaccount :"",
    bankname:""
  },
  bindBankChange: function (e) {
    // 
    console.log(e,'22222222222222222')
    var that = this;
    that.setData({
      bank: that.data.bankArr[e.detail.value]
    });
  },
  changeInputData: function (e) {
    var that = this;
    var inputName = e.currentTarget.dataset.name;
    var inputVlaue = e.detail.value;
    if (inputName == "bank") {
      console.log(inputVlaue)
      that.setData({
        bank: inputVlaue
      });
    }
    if (inputName == "bankName") {
      that.setData({
        bankname: inputVlaue
      });
    }
    if (inputName == "bankAccount") {
      that.setData({
        bankaccount: inputVlaue
      });
    }
  },
  onShow:function(){
    var that=this
    app.request({
      url: api.bank.index,
      data:{
        bid:wx.getStorageSync('bid')
      },
      success:function(e){
        console.log(e,'2222222222222222222')
        if(e.code==1){
          that.setData({
            bankArr: e.data
          })
        }
      }
    })
  
  },
  sendRequest: function () {
    var that = this;
    if (that.validate()) {
    
      // TODO 发送请求
      app.request({
        url: api.bank.bankadd,
        method:'POST',
        data:{
          banknum: that.data.bankaccount,
          bankid: that.data.bank.id,
          real_name: that.data.bankname,
        },
        success:function(e){
          if (e.code==1){
            wx.showToast({
              title: '添加成功',
              duration:1000,
              success:function(){
                setTimeout(function(){
                  wx.navigateBack({
                    delta:1
                  })
                },1000)
              }
            })
          }else{
            wx.showModal({
              title: '提示',
              content: e.msg,
              showCancel:false
            })
          }
        }
      })
 
    }
  },
  validate: function () {
    var that = this;

    if (!that.data.bank || that.data.bank == '') {
      wx.showToast({
        title: '请选择开户行',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.bankname || that.data.bankname == '') {
      wx.showToast({
        title: '开户人不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    if (!that.data.bankaccount || that.data.bankaccount == '') {
      wx.showToast({
        title: '账号不得为空',
        icon: 'none',
        mask: true,
      });
      return false;
    }
    return true;
  },
 
})